package org.itboys.portal.service.manager;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.time.TimeUtils;
import org.itboys.commons.utils.tools.ImagePathUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.member.entity.Member;
import org.itboys.member.service.MemberService;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.portal.dto.mobile.MobiInfoCommentResult;
import org.itboys.portal.dto.mobile.MobiInfoGoodResult;
import org.itboys.portal.entity.PortalInfoComment;
import org.itboys.portal.service.PortalInfoCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/** 
 * @author 作者 huml 
 * @version 创建时间：2016-3-30 下午2:49:15 
 * 手机端资讯评论点赞业务 
 */
@Service
public class MobiPortalInfoCommentManager {

	@Autowired
	private PortalInfoCommentService commentService;
	@Autowired
	private MobiPortalInfoManager portalInfoManager;
	@Autowired
	private MemberService memberService;
	@Autowired
	private ResourceHolder resourceHolder;
	
	/**
	 * 是否赞过这条动态
	 * @param infoId
	 * @param memberId
	 * @return
	 */
	public boolean isGood(Long infoId, Long memberId){
		Map<String, Object> param = Maps.newHashMapWithExpectedSize(3);
		param.put("infoId", infoId);
		param.put("objId", memberId);
		param.put("type", PortalInfoComment.TYPE_GOOD);
		List<PortalInfoComment> list = commentService.list(param);
		return list != null && !list.isEmpty();
	}
	/**
	 * 评论某一条动态  或者回复评论
	 * @param infoId  动态id
	 * @param memberId  评论人id
	 * @param parentCommentId  父评论id  如果是评论动态的话传0
	 * @param content  评论内容
	 * @return
	 */
	public MobiInfoCommentResult doComment(Long infoId, Long memberId, Long parentCommentId, String content, List<String> images){
		// 评论
		PortalInfoComment comment = new PortalInfoComment();
		comment.setInfoId(infoId);
		comment.setType(PortalInfoComment.TYPE_COMMENT);
		comment.setObjId(memberId);
		comment.setContent(content);
		comment.setParentId(parentCommentId);
		comment.setImages(images);
		if(parentCommentId != 0L){
			PortalInfoComment dynamicComment = commentService.getById(parentCommentId);
			comment.setParentObjId(dynamicComment.getObjId());
		}
		commentService.saveWithoutLogin(comment);
		//评论数加一
		portalInfoManager.addCommentCount(infoId);
		return createCommentResult(comment);
	}
	/**
	 * 点赞
	 * @param infoId  动态id
	 * @param memberId  点赞人id
	 * @return
	 */
	public List<String> doGood(Long infoId,Long memberId){
		//首先判断是否赞过这条动态
		Map<String, Object> param = Maps.newHashMapWithExpectedSize(3);
		param.put("type", PortalInfoComment.TYPE_GOOD);
		param.put("objId", memberId);
		param.put("infoId", infoId);
		List<PortalInfoComment> list = commentService.list(param);
		if(list != null && !list.isEmpty()){//已赞过,取消赞
			commentService.delete(list.get(0).getId());
			//点赞数减一
			portalInfoManager.subGoodCount(infoId);
		}else {
			PortalInfoComment comment = new PortalInfoComment();
			comment.setInfoId(infoId);
			comment.setType(PortalInfoComment.TYPE_GOOD);
			comment.setObjId(memberId);
			commentService.saveWithoutLogin(comment);
			//点赞数加一
			portalInfoManager.addGoodCount(infoId);
		}
		List<PortalInfoComment> comments = getByinfoIdAndType(infoId, PortalInfoComment.TYPE_GOOD);
		List<String> goodNames = Lists.newArrayListWithExpectedSize(10);
		for (int i = 0; i < comments.size(); i++) {
			if(i < 10){
				PortalInfoComment comment = comments.get(i);
				Member member = memberService.getById(comment.getObjId());
				if(member!=null){
					goodNames.add(StringUtils.isBlank(member.getNickname())?member.getUsername():member.getNickname());
				}
			}
		}
		return goodNames;
	}
	
	/**
	 * 获取某条动态的评论或点赞
	 * @param infoId
	 * @param type
	 * @return
	 */
	public List<PortalInfoComment> getByinfoIdAndType(Long infoId, Integer type){
		Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
		param.put("infoId", infoId);
		param.put("type", type);
		return commentService.list(param);
	}
	/**
	 * 分页获取动态的评论
	 * @param infoId
	 * @param request
	 * @return
	 */
	public List<MobiInfoCommentResult> commentResults(Long infoId, HttpServletRequest request){
		Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
		param.put("infoId", infoId);
		param.put("type", PortalInfoComment.TYPE_COMMENT);
		Page<PortalInfoComment> page = commentService.pageQuery2(request, param);
		List<MobiInfoCommentResult> results = Lists.newArrayListWithExpectedSize(page.getData().size());
		for (PortalInfoComment comment : page.getData()) {
			MobiInfoCommentResult result = createCommentResult(comment);
			results.add(result);
		}
		return results;
	}
	
	/**
	 * 手机端获取某条动态的评论
	 * @param infoId
	 * @return
	 */
	public List<MobiInfoCommentResult> commentResults(Long infoId){
		List<PortalInfoComment> list = getByinfoIdAndType(infoId, PortalInfoComment.TYPE_COMMENT);
		List<MobiInfoCommentResult> results = Lists.newArrayListWithExpectedSize(list.size());
		for (PortalInfoComment comment : list) {
			MobiInfoCommentResult result = createCommentResult(comment);
			results.add(result);
		}
		return results;
	}
	/**
	 * 创建评论的result
	 * @param comment
	 * @return
	 */
	private MobiInfoCommentResult createCommentResult(PortalInfoComment comment) {
		MobiInfoCommentResult result = new MobiInfoCommentResult();
		result.setInfoId(comment.getInfoId());
		result.setId(comment.getId());
		result.setParentId(comment.getParentId());
		result.setContent(comment.getContent());
		result.setMemberId(comment.getObjId());
		Member member = memberService.getById(comment.getObjId());
		result.setTime(TimeUtils.format(comment.getCreateTime(), CommonConstants.DATE.FORMAT_DEFAULT));
		result.setMemberUserName(member.getUsername());
		if (member != null) {
			result.setMemberName(StringUtils.isBlank(member.getNickname()) ? member.getMobile() : member.getNickname());
			String logo = member.getLogo();
			ImagePathUtils.add(resourceHolder.getStringValue("imgRoot"), logo);
			result.setMemberLogo(logo);
		}
		if(comment.getParentObjId() != null && comment.getParentObjId() > 0L){
			Member parentMember = memberService.getById(comment.getParentObjId());
			if(parentMember!=null){
				result.setParentMemberName(parentMember.getUsername());
			}
		}
		List<String> images = comment.getImages();
		List<String> iii = Lists.newArrayList();
		if(images != null && !images.isEmpty()){
			for (String string : images) {
				iii.add(resourceHolder.getStringValue("imgRoot")+string);
			}
		}
		result.setImages(iii);
		return result;
	}
	/**
	 * 手机端获取某条动态的点赞
	 * @param infoId
	 * @return
	 */
	public List<MobiInfoGoodResult> goodResults(Long infoId){
		List<PortalInfoComment> list = getByinfoIdAndType(infoId, PortalInfoComment.TYPE_GOOD);
		List<MobiInfoGoodResult> results = Lists.newArrayListWithExpectedSize(list.size());
		for (PortalInfoComment comment : list) {
			MobiInfoGoodResult result = new MobiInfoGoodResult();
			result.setId(comment.getId());
			result.setMemberId(comment.getObjId());
			Member member = memberService.getById(comment.getObjId());
			if (member != null) {
				result.setMemberName(StringUtils.isBlank(member.getNickname()) ? member.getMobile() : member.getNickname());
			}
			results.add(result);
		}
		return results;
	}
	
}
